<?php
session_start();

//require_once("includes/connect.php");
//require_once("includes/function.php");

$panngan2_id = $_REQUEST["panngan2_id"];
$panngan3_id = $_REQUEST["panngan3_id"];
?>

<script type="text/javascript">
    $(document).ready(function() {

        $("input,select,textarea").not("[type=submit]").jqBootstrapValidation({
            submitSuccess: function($form, event) {
                //event.preventDefault();

            }
        });

        $("#panngan2_id").change(function() {
            var panngan2 = $(this).val();
            var panngan3 = "<?php echo $panngan3_id; ?>";

            if (panngan2 != "") {
                $.post('dd_panngan_1.php', {panngan2: panngan2, panngan3: panngan3}, function(data) {
                    $("#dd_panngan3").html(data);
                    //alert(data);
                });
            }
        });

        $("#panngan2_id").trigger("change");

        $("#month_year").datepicker({
            dateFormat: "yy-mm"

        });
    });
</script>

<?php
//echo $panngan3;

$submit = $_REQUEST["submit"];

$budget_year = $_REQUEST["budget_year"];

$month_year = $_REQUEST["month_year"];
?>

<form id="frm_init" name="frm_init" method="post" action="index.php" novalidate="">
    <input type="hidden" name="pages" value="frm_init">
    <div class="row-fluid">
        <div class="control-group span1">
            <label class="control-label" for="budget_year">ปีงบประมาณ</label>
            <div class="controls ">
                <select id="budget_year" class="span12" name="budget_year" <?php echo ($id) ? "readonly" : ""; ?>>

                    <?php
                    $this_y = date("Y");

                    if ($this_y < 2556)
                        $this_y += 543;

                    if (!$budget_year)
                        $budget_year = $this_y;

                    for ($i = 2556; $i <= $this_y + 1; $i++) {
                        if ($i == $budget_year) {
                            $selected = "selected";
                        } else {
                            $selected = "";
                        }
                        echo "<option value='$i' $selected>" . $i . "</option>";
                    }
                    ?>
                </select>

            </div>
        </div>

        <div class="control-group span4">
            <label class="control-label" for="แผนงาน">แผนงาน</label>
            <div class="controls ">
                <select class="span12" id="panngan2_id" name="panngan2_id" required>
                    <option value="">เลือก...</option>
                    <?php
                    $db = NEW myDBS;
                    $db->selectDB("hr_udoncity");

                    $sql = "SELECT DISTINCT panngan2_id 
                                FROM position 
                                WHERE lvl_id like '" . $_SESSION["lvl1_id"] . "%'
                                AND panngan2_id IS NOT NULL"
                    ;

                    $result = $db->runSQL($sql);

                    while ($rows = $db->fetch_object($result)) {
                        $db->selectDB("account");

                        $sql = "SELECT DISTINCT * 
                                FROM panngan2 
                                WHERE panngan2_id = '" . $rows->panngan2_id . "'"
                        ;

                        $result1 = $db->runSQL($sql);
                        while ($rows1 = $db->fetch_object($result1)) {
                            if ($rows1->panngan2_id == $panngan2_id) {
                                $selected = " selected";
                            } else {
                                $selected = "";
                            }
                            echo "<option value='" . $rows1->panngan2_id . "' $selected>$rows1->panngan2_name</option>";
                        }
                    }
                    ?>
                </select>
            </div>
        </div><!--/span-->

        <div class="control-group span3">
            <label class="control-label" for="งาน">งาน</label>
            <div class="controls " id="dd_panngan3">
                <select class="span12" id="panngan3_id" name="panngan3_id" required>
                    <option value="">เลือก...</option>

                </select>
            </div>
        </div><!--/span-->

        <div class="control-group span2">
            <label class="control-label" for="month_year">เดือน/ปี</label>
            <div class="controls">
                <input class="span12" type="text" id="month_year" name="month_year" value="<?php echo $month_year; ?>" required />
            </div>
        </div>

        <div class="control-group span2 ">
            <label class="control-label">&nbsp;</label>
            <div class="controls  ">
                <button type="submit" id="submit" name="submit" class="btn btn-primary"><i class="icon-ok icon-white"></i> ตกลง</button>
                <a href="index.php?pages=home" role="button" class="btn"><i class="icon-remove"></i>ยกเลิก</a>
            </div>
        </div>
    </div><!-- <div class="row-fluid"> -->


</form>

<script type="text/javascript">
    $(document).ready(function() {
        $('#example').dataTable({
            "iDisplayStart": "<?php echo 0; ?>",
            "iDisplayLength": 100
        });
    });

    function edit_init(id, salary) {

        var month_year = "<?php echo $month_year; ?>";
        var panngan2 = "<?php echo $panngan2_id; ?>";
        var panngan3 = "<?php echo $panngan3_id; ?>";
        var budget_year = "<?php echo $budget_year; ?>";

        $('<div>').dialog2({
            autoOpen: true,
            title: "แก้ไขข้อมูลเงินเดือน",
            content: "dlg_edit_salary.php?id=" + id + "&month_year=" + month_year + "&panngan2=" + panngan2 + "&panngan3=" + panngan3 + "&salary=" + salary + "&budget_year=" + budget_year,
            id: "modal-edit-salary",
            //	height:140
            //resizable: false,
            //width:1000
        });

    }

    function info_salary(id) {
        //alert(id);
        var month_year = "<?php echo $month_year; ?>";
        var panngan2 = "<?php echo $panngan2_id; ?>";
        var panngan3 = "<?php echo $panngan3_id; ?>";
        var budget_year = "<?php echo $budget_year; ?>";

        $('<div>').dialog2({
            autoOpen: true,
            title: "ข้อมูลเงินเดือน",
            content: "info_salary.php?id=" + id + "&month_year=" + month_year + "&panngan2=" + panngan2 + "&panngan3=" + panngan3 + "&budget_year=" + budget_year,
            id: "modal-info-salary",
            //	height:140
            //resizable: false,
            //width:1000
        });
    }
</script>
<?php
if (isset($submit)) {
    $db1 = NEW myDBS;

    $db->selectDB("hr_udoncity");

    $sql = "SELECT position.id, MAX( position_history_date ) 
        FROM position, position_history
        WHERE position_history.id = position.id
        AND panngan2_id = '$panngan2_id' 
        AND panngan3_id = '$panngan3_id'
        GROUP BY position.id
				";

    //echo $sql;
    $results = $db->runSQL($sql);
    ?>
    <table id="example" class="table table-bordered" width="100%" >
        <caption><br></caption>
        <thead>
            <tr>
                <th>#</th>
                <th>เลขประจำตัว</th>
                <th>ชื่อ - นามสกุล</th>
                <th>เงินเดือน</th>
                <th>action</th>
                <th>ข้อมูล</th>
            </tr>
        </thead>
        <tbody>
            <?php
            $i = 0;
            //$db1->selectDB("account");
            while ($rows = $db->fetch_object($results)) {
                $db->selectDB("hr_udoncity");
                $sql = "SELECT person.id, fname, lname, salary FROM person, position_history
                        WHERE position_history.id = person.id
                        AND person.id = '" . $rows->id . "'
                        ";
                $results1 = $db->runSQL($sql);

                $rows1 = $db->fetch_object($results1);
                $i++;
                echo "<tr>
                        <td>$i</td>
                        <td>$rows1->id</td>
                        <td>$rows1->fname&nbsp;&nbsp;&nbsp;$rows1->lname</td>
                        <td align='right'>" . number_format($rows1->salary, 2) . "</td>
                        <td><button type='button' class='btn' onclick='javascript:edit_init($rows1->id, $rows1->salary)'>Click</button></td>
				  ";


                $sql1 = "SELECT * FROM tbl_init
			WHERE card_id = '$rows1->id'
			AND panngan2_id = '$panngan2_id'
			AND panngan3_id = '$panngan3_id'
			AND month_year = '$month_year'
			AND budget_year = '$budget_year'

			";

                $db1->selectDB("account");
                $results3 = $db1->runSQL($sql1);
                $numrows = $db1->num_rows($results3);
                if ($numrows <= 0) {


                    $sql = "INSERT INTO tbl_init (lvl1_id,
			card_id ,	panngan2_id ,	panngan3_id ,	month_year ,	salary ,	budget_year
			)
			VALUES ('" . $_SESSION["lvl1_id"] . "',
			 '$rows1->id',  '$panngan2_id',  '$panngan3_id',  '$month_year',  '$rows1->salary',  '$budget_year'
			)
			";

                    $db1->insData($sql);
                }
                echo "<td><a href='javascript:info_salary($rows1->id)'>ดู </a></td>
				  </tr>
				  ";
            }
            ?>
        </tbody>
    </table>
    <?php
}
?>
